package cn.edu.sig.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.sig.bean.History;
import cn.edu.sig.bean.Message;
import cn.edu.sig.bean.User;
import cn.edu.sig.service.HistoryService;
import cn.edu.sig.service.MessageService;

@Controller
public class CommonController {

	@Resource(name = "messageService")
	private MessageService messageService;

	@Resource(name = "historyService")
	private HistoryService historyService;

	@RequestMapping(value = "main.html", method = RequestMethod.GET)
	public String mainPage(Model model) {

		List<Message> messageList = messageService.findAll();
		List<History> historyList = historyService.findAll();
		model.addAttribute("messageList", messageList);
		model.addAttribute("historyList", historyList);
		return "main";
	}

	@RequestMapping(value = "login.html", method = RequestMethod.GET)
	public String loginPage() {
		return "login";
	}

	@RequestMapping(value = "publish.html", method = RequestMethod.GET)
	public String publishPage(HttpServletRequest request, Model model) {
		UserDetails userDetails = (UserDetails) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		User user = new User();
		user.setCode(userDetails.getUsername());
		
		request.getSession().setAttribute("user",user);
		List<Message> messageList = messageService.findAll();
		List<History> historyList = historyService.findAll();
		model.addAttribute("messageList", messageList);
		model.addAttribute("historyList", historyList);

		return "publish";
	}

	/*
	 * @RequestMapping(value="viewImg.html",method = RequestMethod.POST) public
	 * String viewImgPage(){ return "/goods/viewImg"; }
	 */

}
